function ApplicationWindow() {
	// Declare module dependencies
	var AddEditVehicleWindow = require('ui/AddEditVehicleWindow'),
		VehicleChartWindow = require('ui/VehicleChartWindow');
	
	var VehiclesView = require('ui/VehiclesView'),
		VehicleDetailView = require('ui/VehicleDetailView'),
		AddVehicleView = require('ui/AddVehicleView'),
		EditVehicleView = require('ui/EditVehicleView'),
		AddVehicleFillUpView = require('ui/AddVehicleFillUpView'),
		EditVehicleFillUpView = require('ui/EditVehicleFillUpView'),
		AddVehicleFillUpDateView = require('ui/AddVehicleFillUpDateView');
		
	// Create object instance
	var self = Ti.UI.createWindow({
		backgroundColor:'#ffffff'
	});
	
	// Create windows
	var vehiclesWindow = Ti.UI.createWindow({
		title:'Vehicles',
		barColor:'#3F4C6B'
	});
	
	// Create iOS specific NavGroup UI
	var navGroup = Ti.UI.iPhone.createNavigationGroup({
		window:vehiclesWindow
	});
	
	// Add this window to navgroup
	self.add(navGroup);
	
	// Create rest of windows
	var vehicleDetailWindow = Ti.UI.createWindow({barColor:'#3F4C6B'});
	
	var addVehicleWindow = new AddEditVehicleWindow(navGroup, 0);
	
	var editVehicleWindow = new AddEditVehicleWindow(navGroup, 1);
	
	var addVehicleFillUpWindow = Ti.UI.createWindow({barColor:'#3F4C6B'});
	
	var editVehicleFillUpWindow = Ti.UI.createWindow({barColor:'#3F4C6B'});
	
	var datePickerWindow = Ti.UI.createWindow({barColor:'#3F4C6B'});
	
	var datePickerWindow2 = Ti.UI.createWindow({barColor:'#3F4C6B'});
	
	// Construct views
	var vehiclesView = new VehiclesView(vehiclesWindow),
		vehicleDetailView = new VehicleDetailView(vehicleDetailWindow),
		addVehicleView = new AddVehicleView(),
		editVehicleView = new EditVehicleView(),
		addVehicleFillUpView = new AddVehicleFillUpView(),
		editVehicleFillUpView = new EditVehicleFillUpView();
	
	// Datepicker view. It gives info to addVehicleFillUpView
	var addFillUpDateView = new AddVehicleFillUpDateView(addVehicleFillUpView);
	var editFillUpDateView = new AddVehicleFillUpDateView(editVehicleFillUpView);
	
	vehiclesWindow.add(vehiclesView);
	vehicleDetailWindow.add(vehicleDetailView);
	addVehicleWindow.add(addVehicleView);
	editVehicleWindow.add(editVehicleView);
	addVehicleFillUpWindow.add(addVehicleFillUpView);
	editVehicleFillUpWindow.add(editVehicleFillUpView);
	datePickerWindow.add(addFillUpDateView);
	datePickerWindow2.add(editFillUpDateView);
	
	// Each time we list vehicles, we need the entire table to be refreshed
	vehiclesWindow.addEventListener('focus', function(e) {
		vehiclesView.fireEvent('updateTableEvent');
	});
	
	// Add behavior for vehicles view
	vehiclesView.addEventListener('vehicleSelected', function(e) {
		// Find associated costs with the vehicle
		var Vehicle = require('models/Vehicle');
		var costsForVehicle = Vehicle.findVehicleCostsPerVehicleId(e.vehicleId);
		for(var key in costsForVehicle) {
			e[key] = costsForVehicle[key];	
		}
		
		// Fire events that vehicle has been selected
		vehicleDetailView.fireEvent('vehicleSelected',e);
		addVehicleFillUpView.fireEvent('vehicleSelected', e);
		
		navGroup.open(vehicleDetailWindow);
		vehicleDetailWindow.setTitle(e.vehicleName);
	});
	
	vehiclesView.addEventListener('addNewVehicleEvent', function(e) {
		// We go to the add vehicle window
		navGroup.open(addVehicleWindow);
		
		// If cancel is clicked, we go back to the vehicle list
		var cancelButton = Titanium.UI.createButton({
			systemButton:Titanium.UI.iPhone.SystemButton.CANCEL
		});
		cancelButton.addEventListener('click', function(e){
			navGroup.close(addVehicleWindow);
		});
		
		// If done is clicked, we save and then go back to vehicle list
		var doneButton = Titanium.UI.createButton({
			systemButton:Titanium.UI.iPhone.SystemButton.DONE
		})
		doneButton.addEventListener('click', function(e){
			addVehicleView.fireEvent('wantToSaveVehicleEvent');
		});
		
		// We change the left and right navigation buttons
		addVehicleWindow.leftNavButton = cancelButton;
		addVehicleWindow.rightNavButton = doneButton;
		
		// We reset the form
		addVehicleView.fireEvent('resetEvent');
	});
	
	vehicleDetailView.addEventListener('addNewVehicleFillUpEvent', function(e) {
		// We go to the add vehicle fillup window
		navGroup.open(addVehicleFillUpWindow);
		
		addVehicleFillUpWindow.setTitle(e.vehicleName);
		
		// If cancel is clicked, we go back to the vehicle
		var cancelButton = Titanium.UI.createButton({
			systemButton:Titanium.UI.iPhone.SystemButton.CANCEL
		});
		cancelButton.addEventListener('click', function(e){
			navGroup.close(addVehicleFillUpWindow);
		});
		
		// If done is clicked, we save and then go back to vehicle list
		var doneButton = Titanium.UI.createButton({
			systemButton:Titanium.UI.iPhone.SystemButton.DONE
		})
		doneButton.addEventListener('click', function(e){
			addVehicleFillUpView.fireEvent('wantToSaveVehicleFillUpEvent');
		});
		
		// We change the left and right navigation buttons
		addVehicleFillUpWindow.leftNavButton = cancelButton;
		addVehicleFillUpWindow.rightNavButton = doneButton;
		
		// Lastly, we want the form and pickers to reset
		addVehicleFillUpView.fireEvent('resetEvent');
		addFillUpDateView.fireEvent('resetEvent');
	});
	
	vehicleDetailView.addEventListener('editVehicleFillUpEvent', function(e) {
		// We go to the add vehicle fillup window
		navGroup.open(editVehicleFillUpWindow);
		
		editVehicleFillUpWindow.setTitle(e.vehicleName);
		
		// If cancel is clicked, we go back to the vehicle
		var cancelButton = Titanium.UI.createButton({
			systemButton:Titanium.UI.iPhone.SystemButton.CANCEL
		});
		cancelButton.addEventListener('click', function(e){
			navGroup.close(editVehicleFillUpWindow);
		});
		
		// If done is clicked, we save and then go back to vehicle list
		var doneButton = Titanium.UI.createButton({
			systemButton:Titanium.UI.iPhone.SystemButton.DONE
		})
		doneButton.addEventListener('click', function(e) {
			editVehicleFillUpView.fireEvent('wantToUpdateVehicleFillUpEvent');
		});
		
		// We change the left and right navigation buttons
		editVehicleFillUpWindow.leftNavButton = cancelButton;
		editVehicleFillUpWindow.rightNavButton = doneButton;
		
		// Lastly, we want the form and pickers to reset
		editVehicleFillUpView.fireEvent('resetEvent', e);
		editFillUpDateView.fireEvent('resetEvent');
	});
	
	vehicleDetailView.addEventListener('wantToViewFuelConsumptionChart', function(e) {
		var vehicleChartWindow = new VehicleChartWindow(navGroup, e.vehicleId, 'consumption');
		navGroup.open(vehicleChartWindow);
	});
	
	vehicleDetailView.addEventListener('wantToViewFuelEconomyChart', function(e) {
		var vehicleChartWindow = new VehicleChartWindow(navGroup, e.vehicleId, 'economy');
		navGroup.open(vehicleChartWindow);
	});
	
	// Edit vehicle
	vehicleDetailView.addEventListener('wantToEditVehicle', function(e) {
		navGroup.open(editVehicleWindow);
		
		// If cancel is clicked, we go back to the vehicle list
		var cancelButton = Titanium.UI.createButton({
			systemButton:Titanium.UI.iPhone.SystemButton.CANCEL
		});
		cancelButton.addEventListener('click', function(e){
			navGroup.close(editVehicleWindow);
		});
		
		// If done is clicked, we save and then go back to vehicle list
		var doneButton = Titanium.UI.createButton({
			systemButton:Titanium.UI.iPhone.SystemButton.DONE
		})
		doneButton.addEventListener('click', function(e2){
			editVehicleView.fireEvent('wantToUpdateVehicleEvent', e);
		});
		
		// We change the left and right navigation buttons
		editVehicleWindow.leftNavButton = cancelButton;
		editVehicleWindow.rightNavButton = doneButton;
		
		// We reset the form
		editVehicleView.fireEvent('resetEvent', e);
	});
	
	// Delete vehicle
	vehicleDetailView.addEventListener('wantToDeleteVehicle', function(e) {
		var deleteOptions = ['Delete the vehicle', 'Cancel'];
		var deleteVehicleDialog = Titanium.UI.createOptionDialog({
	    	title: 'Are you absolutely sure?',
	    	options: deleteOptions,
	    	cancel:1
		});
		deleteVehicleDialog.addEventListener('click', function(deleteEvent) {
			if(deleteEvent.index == 0) {
				try {
					var Vehicle = require('models/Vehicle');
					Vehicle.remove(e.vehicleId);
					navGroup.close(vehicleDetailWindow);
					vehiclesView.fireEvent('updateTableEvent');
				} catch(e) {
					Ti.UI.createAlertDialog({
		    			message: e,
		    			ok: 'Ok',
		    			title: 'Could not delete vehicle'
		    		}).show();	
				}
			}
		});
		deleteVehicleDialog.show();
	});
	
	// Reset fillups
	vehicleDetailView.addEventListener('wantToResetVehicleFillups', function(e) {
		var resetOptions = ['Remove all fill-ups', 'Cancel'];
		var resetVehicleFillupsDialog = Titanium.UI.createOptionDialog({
	    	title: 'Are you absolutely sure?',
	    	options: resetOptions,
	    	cancel:1
		});
		resetVehicleFillupsDialog.addEventListener('click', function(resetEvent) {
			if(resetEvent.index == 0) {
				try {
					var VehicleFillup = require('models/VehicleFillup');
					VehicleFillup.removeAll(e.vehicleId);
					navGroup.close(vehicleDetailWindow);
					vehiclesView.fireEvent('updateTableEvent');
				} catch(e) {
					Ti.UI.createAlertDialog({
		    			message: e,
		    			ok: 'Ok',
		    			title: 'Could not remove all fillups'
		    		}).show();	
				}
			}
		});
		resetVehicleFillupsDialog.show();
	});
	
	addVehicleFillUpView.addEventListener('showDatePickerEvent', function(e) {
		navGroup.open(datePickerWindow);
		
		// If cancel is clicked, we go back to the vehicle
		var cancelButton = Titanium.UI.createButton({
			systemButton:Titanium.UI.iPhone.SystemButton.CANCEL
		});
		cancelButton.addEventListener('click', function(e){
			navGroup.close(datePickerWindow);
		});
		
		// If done is clicked, we save and then go back to vehicle list
		var doneButton = Titanium.UI.createButton({
			systemButton:Titanium.UI.iPhone.SystemButton.DONE
		})
		doneButton.addEventListener('click', function(e2) {
			// Done button was clicked, so we ask the datepicker what the selected date is
			addFillUpDateView.fireEvent('requestedPickedValueEvent');
			navGroup.close(datePickerWindow);
		});
		
		// We change the left and right navigation buttons
		datePickerWindow.leftNavButton = cancelButton;
		datePickerWindow.rightNavButton = doneButton;
	});
	
	addVehicleFillUpView.addEventListener('saveVehicleFillUpEvent', function(e) {
		try {
			var newVehicleFillup = require('models/VehicleFillup');
			var newFillUpId = newVehicleFillup.save(
				e.vehicleId,
				e.odometer,
				e.quantity,
				e.price,
				e.date,
				e.isPartial
			);
			
			var newVehicleConsumption = require('models/VehicleConsumption');
			var latestFuelConsumption = newVehicleConsumption.save(
				e.vehicleId,
				newFillUpId
			);
			
			// We alter the event so that the detail view can pass it on to the stats view
			var Conversions = require('tools/Conversions');
			
			e.averageFuelConsumption = Conversions.fillupUnitsPerDistanceFormat(latestFuelConsumption);
			e.averageFuelEconomy = Conversions.fillupUnitsPerDistanceToDistancePerFillupUnit(latestFuelConsumption);
			
			// We also add the updated vehicle costs to thsi event
			// TODO findVehicleCostsPerVehicleId
		} catch(e) {
			Ti.UI.createAlertDialog({
    			message: e,
    			ok: 'Ok',
    			title: 'Sorry...'
    		}).show();	
		}
		
		// We send the vehicleSelected event to the detail view, as this automatically
		// refreshes the fillup table
		vehicleDetailView.fireEvent('vehicleSelected', e);
		navGroup.close(addVehicleFillUpWindow);
	});
	
	editVehicleFillUpView.addEventListener('showDatePickerEvent', function(e) {
		navGroup.open(datePickerWindow2);
		
		// If cancel is clicked, we go back to the vehicle
		var cancelButton = Titanium.UI.createButton({
			systemButton:Titanium.UI.iPhone.SystemButton.CANCEL
		});
		cancelButton.addEventListener('click', function(e){
			navGroup.close(datePickerWindow2);
		});
		
		// If done is clicked, we save and then go back to vehicle list
		var doneButton = Titanium.UI.createButton({
			systemButton:Titanium.UI.iPhone.SystemButton.DONE
		})
		doneButton.addEventListener('click', function(e2) {
			// Done button was clicked, so we ask the datepicker what the selected date is
			editFillUpDateView.fireEvent('requestedPickedValueEvent');
			navGroup.close(datePickerWindow2);
		});
		
		// We change the left and right navigation buttons
		datePickerWindow2.leftNavButton = cancelButton;
		datePickerWindow2.rightNavButton = doneButton;
	});
	
	editVehicleFillUpView.addEventListener('updateVehicleFillUpEvent', function(e) {
		
		try {
			var VehicleFillupModel = require('models/VehicleFillup');
			var updatedFillUp = VehicleFillupModel.update(
				e.fillupId,
				e.odometer,
				e.quantity,
				e.price,
				e.date
			);
			
		} catch(e) {
			Ti.UI.createAlertDialog({
    			message: e,
    			ok: 'Ok',
    			title: 'Sorry...'
    		}).show();	
		}
		
		// We send the vehicleSelected event to the detail view, as this automatically
		// refreshes the fillup table	
		vehicleDetailView.fireEvent('vehicleSelected', e);
		vehiclesView.fireEvent('updateTableEvent');
		navGroup.close(editVehicleFillUpWindow);
	});
	
	return self;
};

module.exports = ApplicationWindow;
